<?php
/**
 * Core_Model_Router
 * router model
 *
 * @author Adrian Stolarski
 */
class Core_Model_Router implements Core_Model_Routable, Core_Model_Classinitialisable {
    
    /**
     *
     * @var type $class_compose
     * class compose handler
     */
    public $class_compose;
    
    /**
     *
     * @var type $class_initialise
     * class initialise handler
     */
    public $class_initialise;
    
    /**
     * constructor()
     */
    public function __construct() {
        $this->class_compose = new Core_Data_Composeclassname;
    }
    
    /**
     * route()
     * compose class name and load current class
     * 
     * @param type $query_string
     * @throws Exception
     */
    public function route($query_string) {
        //echo $query_string;
        $class_name = $this->class_compose->compose_class_name($query_string);
        //echo $class_name;
        try {
            if((isset($class_name)) && ($class_name != '')){
                return $this->class_init($class_name);
            }
            else {
                $message = new Core_Data_Classnotexists;
                throw new Exception($message->message($class_name));
                echo '<meta http-equiv="Refresh" content="0;url='.$_SERVER['HTTP_REFERER'].'">';
            }
        } catch (Exception $exception) {
            echo $exception->getMessage();
        }
    }
    
    /**
     * class_init()
     * initialise class
     * 
     * @param type $class_name
     * @throws Exception
     */
    public function class_init($class_name) {
        try {
            if(class_exists($class_name)){
                $this->class_initialise = new Core_Data_Classinit;
                return $this->class_initialise->class_init($class_name);
            }
            else {
                $message = new Core_Data_Classnotexists;
                throw new Exception($message->message($class_name));
                echo '<meta http-equiv="Refresh" content="0;url='.$_SERVER['HTTP_REFERER'].'">';
            }
        } catch (Exception $exception) {
            echo $exception->getMessage();
        }
    }

}
